//
//  SYMyViewController.m
//  BSHOUYIFANG
//
//  Created by 曹雁彬 on 15/8/13.
//  Copyright (c) 2015年 CYB. All rights reserved.
//

#import "SYMyViewController.h"
#import "SYMyTableViewCell.h"
#import "SYLonginTableViewCell.h"
#import "SYContactViewController.h"
#import "SYSetViewController.h"
#import "SYLoginViewController.h"
#import "Header.h"
#import "SYFileHandle.h"
#import "SYUserViewController.h"
#import "SYMyHomepageViewController.h"
#import "SYMyFavoriteViewController.h"
#import "SYMyGroupViewController.h"
#import "SYMymessageViewController.h"
#import "SYMyRemindViewController.h"
#import "SYMyFmdb.h"
@interface SYMyViewController ()<UITableViewDataSource,UITableViewDelegate>
@property(nonatomic,strong)NSArray *imageArray;
@property(nonatomic,strong)NSArray *titleArray;
@property (nonatomic,strong)SYMainBaseViewController *baseController;
@property (nonatomic,strong)NSArray *arrForSection1;
@property (nonatomic,strong)NSArray *arrForSection2;
@property (nonatomic,strong)NSArray *arrForSection3;
@property (nonatomic,strong)NSArray *classArr;
@end

@implementation SYMyViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.tabBarController.tabBar.translucent = YES;
    //[[SYFileHandle shareInstance] setloginState:NO];
    [self navigationItemTitleColor];
    _myTableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, SCREEN_HEIGHT ) style:UITableViewStyleGrouped];
                    
    _myTableView.delegate = self;
    _myTableView.dataSource = self;
    [_myTableView registerClass:[SYMyTableViewCell class] forCellReuseIdentifier:@"my"];
    [_myTableView registerClass:[SYLonginTableViewCell class] forCellReuseIdentifier:@"login"];
    [self.view addSubview:_myTableView];
    
    _arrForSection1 = @[@"SYUserViewController"];
    _arrForSection2 = @[@"SYMyHomepageViewController",@"SYMyFavoriteViewController",@"SYMyGroupViewController",@"SYMymessageViewController",@"SYMyRemindViewController"];
    _arrForSection3 = @[@"SYContactViewController",@"SYSetViewController"];
    _classArr = @[_arrForSection1,_arrForSection2,_arrForSection3];
    
    _titleArray =@[@"我的主页",@"我喜欢的",@"我的圈子",@"我的消息",@"我的提醒"];
    _imageArray=@[@"my_page@3x.png",@"my_like@3x.png",@"my_quan@3x.png",@"my_msg@3x.png",@"my_alert@3x.png"];
}

- (void)navigationItemTitleColor
{
    UILabel *customLab = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 30)];
    [customLab setTextColor:[UIColor blackColor]];
    [customLab setText:@"我 的"];
    customLab.textAlignment = NSTextAlignmentCenter;
    customLab.font = [UIFont boldSystemFontOfSize:17];
    self.navigationItem.titleView = customLab;
}


-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    if (section == 2) {
        return 32;
    }else
    {
        return 10;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section
{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section == 0) {
      return SCREEN_WIDTH/4.5+20;
    }else if(indexPath.section == 1){
        return 50;
    }else{
        return 50;
    }
}

-(NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    if (section == 0)
    {
        return 1;
    }
    else if(section == 1)
    {
        return 5;
    }
    else
    {
        return 2;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    if (indexPath.section ==0)
    {
        SYLonginTableViewCell *longinCell = [tableView dequeueReusableCellWithIdentifier:@"login"];
        /**
         *  如果登陆成功 则使用登陆用户的用户名
         */

        
        BOOL islongin = [[SYFileHandle shareInstance] loginState];
        if (islongin) {
            NSString *str = [[SYFileHandle shareInstance] phoneNumber];
            SYUser *user = [[SYMyFmdb shareInstance]getUserInfophoneNumber:str];
            longinCell.titleLabel.text =user.username;
            longinCell.titleImageView.image = [UIImage imageWithData:[[SYFileHandle shareInstance]icon]];
        }else{
            longinCell.titleLabel.text = @"登陆";
            longinCell.titleImageView.image = [UIImage imageNamed:@"circle_default_face"];
        }
        return longinCell;
    }else
    {SYMyTableViewCell *MyCell = [tableView dequeueReusableCellWithIdentifier:@"my" forIndexPath:indexPath];
        if (indexPath.section ==1)
        {
            MyCell.titleLabel.text = _titleArray[indexPath.row];
            MyCell.imageView.image =[UIImage imageNamed:[NSString stringWithFormat:@"%@",_imageArray[indexPath.row]]];
            
        }
        else if (indexPath.section ==2)
        {
            MyCell.titleLabel.text =@"联系我们";
            MyCell.imageView.image =[UIImage imageNamed:@"my_apply_quan@3x.png"];
            if (indexPath.row ==1)
            {
                MyCell.titleLabel.text =@"设置";
                MyCell.imageView.image = [UIImage imageNamed:@"my_setting@3x.png"];
            }
        }
        return MyCell;
    }
}
//点击我的页面cell 判断是否已经登陆 如果登陆跳到对应页面 否则跳到登陆页面进行登陆
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    BOOL islongin = [[SYFileHandle shareInstance] loginState];
    if (islongin) {
        _baseController = [[NSClassFromString(_classArr[indexPath.section][indexPath.row]) alloc]init];
        _baseController.view.backgroundColor = [UIColor whiteColor];
        
        [self.navigationController pushViewController:_baseController animated:YES];
    }else
    {
        SYLoginViewController *setVc =[[SYLoginViewController alloc] init];
         [self.navigationController pushViewController:setVc animated:YES];
         
    }
    [tableView deselectRowAtIndexPath:indexPath animated:NO];//pop回来颜色消失
}

#pragma mark -------UITableViewCell点击的响应方法------
//进入我的主页
- (void)p_showMyHomepage
{
    BOOL isLogin = [[SYFileHandle shareInstance] loginState];
    if (isLogin) {
        //已登录，进入个人信息页面
        SYMyHomepageViewController *myHomepageVC = [[SYMyHomepageViewController alloc] init];
        [self.navigationController pushViewController:myHomepageVC animated:YES];
        
    }else{
        
        SYLoginViewController *loginVC = [[SYLoginViewController alloc] init];
        
        [self.navigationController pushViewController:loginVC animated:YES];
    }
}

- (void)viewWillAppear:(BOOL)animated
{
    [_myTableView reloadData];

}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
